<?php
/* SVN FILE: $Id: item_secretmessage.php 247 2009-10-22 15:31:39Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Postcard Money item
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 247 $
* @lastrevision $Date: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/items/own/item_secretmessage.php $
*/

class item_secretmessage extends templateItem
{
	/**
	 * The default name of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultName = "Secret Message";
	
	/**
	 * The default description of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultDescription = "Use the item to discover a message.";
	
	// Inherited Properties
	/**
	 * Item setting to allow multiple instances within ibMarket 
	 *
	 * @access	protected
	 * @var		boolean
	 */
	protected $allowMultiple = true;
	
	/**
	 * Item specific setting
	 *
	 * @access	protected
	 * @var		array
	 */
	protected $itemSettings = array();
	
	/**
	 * Constructor to setup the item
	 *
	 * @access	public
	 * @param	ipsRegistry	$registry	IPB registry
	 * @param	array		&$item		The item information
	 */
	public function __construct(ipsRegistry $registry, array &$item)
	{
		parent::__construct($registry, $item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->itemSettings = unserialize($item['item_config']);
		}
		else
		{
			$this->itemSettings['title'] = "Hidden Message";
			$this->itemSettings['destination'] = 'screen';
			$this->itemSettings['message'] = "A secret message for you.";
		}
	}
	
	/**
	 * Additional settings when adding/editing the item
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminForm()
	{
		if ( IPSText::getTextClass('editor')->method == 'rte' )
		{
			$message = IPSText::getTextClass('bbcode')->convertForRTE( $this->itemSettings['message'] );
		}
		else
		{
			$message = IPSText::getTextClass('bbcode')->preEditParse( $this->itemSettings['message'] );
		}
		
		$destination = array(array('screen',$this->lang->words['on_screen']),array('pm',$this->lang->words['by_pm']));
		
		$form['destination'] = ipsRegistry::getClass('output')->formDropdown('item[destination]', $destination, $this->itemSettings['destination']);
		$form['title'] = ipsRegistry::getClass('output')->formInput('item[title]', $this->itemSettings['title']);
		$form['message'] = IPSText::getTextClass('editor')->showEditor( $message, 'item_message' );
		return <<<EOF
				<li>
					<label>{$this->lang->words['secret_msg_destination']}</label>
					{$form['destination']}
				</li>
				<li>
					<label>{$this->lang->words['secret_msg_title']}</label>
					{$form['title']}
				</li>
				<li>
					<label>{$this->lang->words['secret_msg_message']}</label>
					<br />
					{$form['message']}
				</li>

EOF;
	}
	
	/**
	 * Saving to adding/editing the item
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	array	[$input]		Relavent input relating to the item
	 * @return	void
	 */
	public function adminSave(array &$item_info, array $input)
	{
		$setting = array();
		$setting['destination'] = $input['destination'];
		$setting['title'] = $input['title'];
		$setting['message'] = IPSText::getTextClass('bbcode')->preDbParse(IPSText::getTextClass('editor')->processRawPost('item_message'));
		
		$item_info['item_config'] = serialize($setting);
	}
	
	/**
	 * When quick saving the price and stock
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	string	$type			Possible values: 'stockprice', more possibly in the future
	 * @return	void
	 */
	public function adminQuickSave(array &$item_info, $type)
	{
		return;
	}
	
	/**
	 * When removing the item from ibMarket
	 *
	 * @access	public
	 * @return	void
	 */
	public function adminRemove()
	{
		return;
	}
	
	/**
	 * Viewing the file edits if any
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminViewFileEdits()
	{
		return false;
	}
	
	/**
	 * Any admin AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function adminAjax()
	{
		return;
	}
	
	/**
	 * Check and modify the intentory item information before storing in the database
	 *
	 * @access	public
	 * @param	array	&$invItem		The new inventory item information to be stored in the database.
	 * @return	bool	true if the item is successfully created, false otherwise
	 */
	public function publicOnCreateItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When using the item
	 * Show a form and validate the form
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	string|bool				string if there is a form to be used, true if successfully used, false otherwise
	 */
	public function publicUseItem(array &$invItem)
	{
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'public_items' ) );
		
		$this->deleteItems($invItem);
		
		$this->writeLog($invItem, "Revealed secret message");
		
		if($this->itemSettings['destination'] == 'pm')
		{
			require_once( IPSLib::getAppDir( 'members' ) . "/sources/classes/messaging/messengerFunctions.php" );
			$messenger = new messengerFunctions($this->registry);
			
			try
			{
				$this->settings['msg_allow_html'] = 0;
				$this->settings['msg_allow_code'] = 1;
				$messenger->sendNewPersonalTopic($this->memberData['member_id'], $this->settings['market_messenger_id'], array(), $this->itemSettings['title'], $this->itemSettings['message'], array('isSystem'=>1,'forcePm'=>1));
			}
			catch(Exception $e)
			{
				$this->registry->getClass('output')->showError($e->getMessage());
			}
			
			$this->registry->getClass('output')->redirectScreen( $this->lang->words['secret_message_sent_by_pm'], $this->settings['base_url_with_app'] . "module=inventory" );
		}
		
		$message = IPSText::getTextClass('bbcode')->preDisplayParse($this->itemSettings['message']);
		
		return <<<EOF
<div id='postcard'class='block_wrap'>
	<h3 id='postcardHeader' class='maintitle'>{$this->item['item_name']}</h3>
	<fieldset class="row2">
		<ul>
			<li>
				<label><strong>{$this->itemSettings['title']}</strong></label>
				<br />
				{$message}
			</li>
		</ul>
	</fieldset>
</div>
EOF;
	}
	
	/**
	 * Any AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function publicAjax(array &$invItem)
	{
		return;
	}
	
	/**
	 * When the item is being sold back
	 *
	 * @access	public
	 * @param	array	&$invItem	The inventory item information
	 * @return	bool				true if successful, false otherwise
	 */
	public function publicOnResoldItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When the item is being restocked
	 *
	 * @access	public
	 * @param	array	&$item	The inventory item information
	 * @return	void
	 */
	public function onItemRestock(array &$item)
	{
		return;
	}
}
